<template>
	<div class="container">
		<h1>🎉登录</h1>
		<el-form class="form" label-width="120px" label-position="top">
			<el-form-item label="ID">
				<el-col :span="16">
					<el-input
						type="text"
						placeholder="请输入ID"
						v-model.trim="formData.id"
					/>
				</el-col>
			</el-form-item>
			<el-form-item label="密码: ">
				<el-col :span="16">
					<el-input
						type="password"
						placeholder="请输入密码"
						v-model.trim="formData.password"
					/>
				</el-col>
			</el-form-item>
			<el-form-item label="身份: ">
				<div>
					<el-radio-group v-model="formData.sf">
						<el-radio label="学生" size="large">学生</el-radio>
						<el-radio label="管理员" size="large">管理员</el-radio>
					</el-radio-group>
				</div>
			</el-form-item>
			<el-button type="primary" @click="login">登录</el-button>
		</el-form>
	</div>
</template>

<script>
import { ElMessage } from "element-plus";

export default {
	name: "StudentLogin",
	data() {
		return {
			register: {
				name: "register",
			},
			formData: {
				id: "",
				password: "",
				sf: null,
			},
		};
	},
	methods: {
		login() {
			if (this.formData.sf === "学生") {
				let requestBody = new URLSearchParams();
				requestBody.append("studentID", this.formData.id);
				requestBody.append("password", this.formData.password);
				fetch(`${this.$store.getters.point}/Student/loginIn/`, {
					method: "POST",
					mode: "cors",
					body: requestBody,
				})
					.then((response) => response.text())
					.then((result) => {
						let data = JSON.parse(result).code;
						if (parseInt(data) === 1) {
							ElMessage({
								message: "登陆成功",
								type: "success",
							});
							let requestBody = new URLSearchParams();
							requestBody.append("id", this.formData.id);
							fetch(
								`${this.$store.getters.point}/Student/preSignUp/getStudent/`,
								{
									method: "POST",
									mode: "cors",
									body: requestBody,
								}
							)
								.then((response) => response.text())
								.then((result) => {
									let data = JSON.parse(result).data;
									this.$store.commit("login", data);
									this.$router.push("/");
								});
						} else {
							ElMessage({
								message: "用户ID或密码错误",
								type: "error",
							});
						}
					});
			} else if (this.formData.sf === "管理员") {
				let requestBody = new URLSearchParams();
				requestBody.append("ID", this.formData.id);
				requestBody.append("password", this.formData.password);
				fetch(`${this.$store.getters.point}/Admin/loginIn`, {
					method: "POST",
					mode: "cors",
					body: requestBody,
				})
					.then((response) => response.text())
					.then((result) => {
						let data = JSON.parse(result).code;
						if (parseInt(data) === 1) {
							ElMessage({
								message: "登陆成功",
								type: "success",
							});
							this.$store.commit("loginAdmin");
							this.$router.push("/manage");
						} else {
							ElMessage({
								message: "用户ID或密码错误",
								type: "error",
							});
						}
					});
			} else {
				ElMessage({
					message: "请选择身份",
					type: "warning",
				});
			}
		},
	},
};
</script>

<style scoped>
.container {
	font-weight: bolder;
	width: 50%;
	margin: auto;
}

.form {
	margin-top: 30px;
}

a {
	text-decoration: none;
}
</style>
